<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        // 实现 add(1)(2)(3) = 6;
        // add(1, 2, 3)(4) = 10;
        // add(1)(2)(3)(4)(5) = 15;
        function add() {

            const numArr = Array.from(arguments);

            const calculate = function () {
                numArr.push(...arguments);
                return calculate;
            }

            calculate.toString = function () {
                return numArr.reduce((a, b) => a + b, 0);
            }

            return calculate;
        }

        console.log(add(1)(2)(3) == 6);

        // 封装柯里化
        function progressCurrying(fn, args) {
            let _this = this,
                len = fn.length,
                args = args || [];

            return function () {  
                let _args = Array.prototype.slice.call(arguments);
                Array.prototype.push.apply(args,_args);
                if(_args.length<len){
                    return progressCurrying(_this,fn,_args);
                }
                return fn.apply(this, _args);
            }
        }
    </script>
</body>

</html>